﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NBA.DBUtility;
using NBA.Entity;
using System.Data.SqlClient;

namespace NBA.DataAccess
{
    public class DaTeamInfo
    {
        NbaDbUtility nbaDbUtility = new NbaDbUtility();

        public int Insert(TeamInfo teamInfo)
        {
            string sql = "INSERT INTO TeamInfo(TeamId,LocalName,TeamName,TeamFullName,TeamEnglishName) " +
                " VALUES(@TeamId,@LocalName,@TeamName,@TeamFullName,@TeamEnglishName)";

            SqlParameter[] sqlParameter = 
            {
                new SqlParameter("@TeamId", teamInfo.TeamId),
                new SqlParameter("@LocalName", 
                    string.IsNullOrEmpty(teamInfo.LocalName)?string.Empty:teamInfo.LocalName),
                new SqlParameter("@TeamName", 
                    string.IsNullOrEmpty(teamInfo.TeamName)? string.Empty:teamInfo.TeamName),
                new SqlParameter("@TeamFullName", 
                    string.IsNullOrEmpty(teamInfo.TeamFullName)? string.Empty: teamInfo.TeamFullName),
                new SqlParameter("@TeamEnglishName",
                    string.IsNullOrEmpty(teamInfo.TeamEnglishName)?string.Empty:teamInfo.TeamEnglishName)
            };

            return nbaDbUtility.ExecuteNonQuery(sql,sqlParameter);
        }

        public Dictionary<Guid, string> GetAllTeam()
        {
            string sql = "SELECT * FROM TeamInfo WHERE IsDeleted = 0";
            return NbaEntityHelper<TeamInfo>.GetEntitySet(sql).ToDictionary(d => d.TeamId, d => d.TeamName);
        }

        public TeamInfo Find(string localName, string teamName)
        {
            string sql = "SELECT * FROM TeamInfo WHERE LocalName = @LocalName AND TeamName = @TeamName";
            SqlParameter[] sqlParameterSet = 
            {
                new SqlParameter("@LocalName", localName),
                new SqlParameter("@TeamName", teamName)
            };
            return NbaEntityHelper<TeamInfo>.GetEntitySet(sql, sqlParameterSet).FirstOrDefault();
        }


        public TeamInfo FindTeamFullName(string teamFullName)
        {
            string sql = "SELECT * FROM TeamInfo WHERE TeamFullName = @TeamFullName";

            SqlParameter sqlParameter = new SqlParameter("@TeamFullName", teamFullName);

            return NbaEntityHelper<TeamInfo>.GetEntitySet(sql, sqlParameter).FirstOrDefault();
        }

        public TeamInfo FindTeamName(string teamName)
        {
            string sql = "SELECT * FROM TeamInfo WHERE TeamName = @TeamName";

            SqlParameter sqlParameter = new SqlParameter("@TeamName", teamName);

            return NbaEntityHelper<TeamInfo>.GetEntitySet(sql, sqlParameter).FirstOrDefault();
        }

        /// <summary>
        /// not update
        /// </summary>
        /// <param name="teamInfo"></param>
        /// <returns></returns>
        public Guid InsertOrUpdate(TeamInfo teamInfo)
        {
            Guid result = new Guid();
            
            TeamInfo tempTeamInfo = null;

            if (!string.IsNullOrEmpty(teamInfo.TeamFullName))
            {
                tempTeamInfo = FindTeamFullName(teamInfo.TeamFullName);
            }
            else if(!string.IsNullOrEmpty(teamInfo.TeamName))
            {
                tempTeamInfo = FindTeamName(teamInfo.TeamName);
            }

            if (tempTeamInfo == null)
            {
                if (Insert(teamInfo) > 0)
                {
                    result = teamInfo.TeamId;
                }
            }
            else
            {
                result = tempTeamInfo.TeamId;
            }

            return result;
        }

        public int Update(TeamInfo teamInfo)
        {
            return 0;
        }
    }
}
